package com.baobao.service;

import com.baobao.dao.BbeventDAO;
import com.baobao.domain.Bbevent;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Bbevent entities
 * 
 */

@Service("BbeventService")
@Transactional
public class BbeventServiceImpl implements BbeventService {

	/**
	 * DAO injected by Spring that manages Bbevent entities
	 * 
	 */
	@Autowired
	private BbeventDAO bbeventDAO;

	/**
	 * Instantiates a new BbeventServiceImpl.
	 *
	 */
	public BbeventServiceImpl() {
	}

	/**
	 * Delete an existing Bbevent entity
	 * 
	 */
	@Transactional
	public void deleteBbevent(Bbevent bbevent) {
		bbeventDAO.remove(bbevent);
		bbeventDAO.flush();
	}

	/**
	 * Save an existing Bbevent entity
	 * 
	 */
	@Transactional
	public void saveBbevent(Bbevent bbevent) {
		Bbevent existingBbevent = bbeventDAO.findBbeventByPrimaryKey(bbevent.getBbId(), bbevent.getTime());

		if (existingBbevent != null) {
			if (existingBbevent != bbevent) {
				existingBbevent.setBbId(bbevent.getBbId());
				existingBbevent.setTime(bbevent.getTime());
				existingBbevent.setType(bbevent.getType());
				existingBbevent.setConfirmPhone(bbevent.getConfirmPhone());
				existingBbevent.setConfirmTime(bbevent.getConfirmTime());
			}
			bbevent = bbeventDAO.store(existingBbevent);
		} else {
			bbevent = bbeventDAO.store(bbevent);
		}
		bbeventDAO.flush();
	}

	/**
	 * Load an existing Bbevent entity
	 * 
	 */
	@Transactional
	public Set<Bbevent> loadBbevents() {
		return bbeventDAO.findAllBbevents();
	}

	/**
	 * Return a count of all Bbevent entity
	 * 
	 */
	@Transactional
	public Integer countBbevents() {
		return ((Long) bbeventDAO.createQuerySingleResult("select count(*) from Bbevent o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public Bbevent findBbeventByPrimaryKey(String bbId, String time) {
		return bbeventDAO.findBbeventByPrimaryKey(bbId, time);
	}

	/**
	 * Return all Bbevent entity
	 * 
	 */
	@Transactional
	public List<Bbevent> findAllBbevents(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Bbevent>(bbeventDAO.findAllBbevents(startResult, maxRows));
	}

	@Transactional
	public Set<Bbevent> findBbeventByDate(String bbId,String startTime, String endTime) {
		return bbeventDAO.findBbeventByTimeBetween(bbId,startTime, endTime);
	}
}
